$(function() {
	$("#search").click(function() {
		var queryParams = $('#datagrid').datagrid('options').queryParams;
		queryParams.search_username = $('#search_mobile').val();
		queryParams.userID = 12;
		queryParams.search.unm='aa';
		$("#datagrid").datagrid('getPager').pagination('select', 1);
	});
	
	var $win = $('#edit-window').window({
		title : '新增',
		width : 430,
		height : 420,
		top : ($(window).height() - 430) * 0.5,
		left : ($(window).width() - 420) * 0.5,
		shadow : true,
		modal : true,
		closed : true,
		minimizable : false,
		maximizable : false,
		collapsible : false
	});
	
	var form = $win.find("form");
	
	$("#add").click(function() {
		$win.window('open');
		$('#edit_usercode').textbox('setValue','');
		$('#edit_username').textbox('setValue','');
		$('#edit_password').textbox('setValue','');
		form.url = '/AQAppServer/user/saveUser';
	});
	
	$("#submitForm").click(function() {
		form.form('submit', {
			url : form.url,
			success : function(data) {
				data = toJson(data);
				if (data.flag == 0) {
					$("#datagrid").datagrid('reload');
					$win.window('close');
				} else {
					$.messager.alert('错误', data.msg, 'error');
				}
			}
		});
	});
	
	$("#clearForm").click(function() {
		form.form("clear");
	});
	
	$("#delete").click(function() {
		var rows = $('#datagrid').datagrid('getSelections');
		if (rows.length == 0) {
			$.messager.alert('警告', '请至少选择一条记录!', 'warning');
			return;
		}
		$.messager.confirm("操作提示","您确定要删除这些数据吗？",function(data) {
				if (data) {
					var userIDs = '';
					for ( var i = 0; i < rows.length; i++) {
						if (i > 0) {
							userIDs += ",";
						}
						userIDs += rows[i].userID;
					}
					$.ajax({
						url : '/AQAppServer/user/deleteUser',
						type : 'POST',
						dataType: 'json',
						data: 'userID=' + userIDs,
						success : function(data) {
							data = toJson(data);
							if (data.flag == 0) {
								$("#datagrid").datagrid('reload');
							} else {
								$.messager.alert('错误',data.flag,'error');
							}
						},
						error : function() {
							$.messager.alert('错误','操作失败!','error');
						}
					});
				}
			});
	});
});